package com.senven.goods.controller.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.senven.common.entity.Goods;
import com.senven.common.vo.CommonResult;
import com.senven.goods.Vo.GoodsVo;
import com.senven.goods.service.IGoodsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gjh
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/api/goods/goods")

public class ApiGoodsController {

    @Autowired
    private IGoodsService iGoodsService;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @ApiOperation(value="根据店铺id显示所有的菜品")
    @PostMapping("/selectAll")
    public CommonResult selectbranchid(Integer branchid, String name, Integer typeid, HttpServletRequest request){
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");
        System.out.println(name);
        return iGoodsService.selectbranchid(branchid,name,typeid,id);
    }
    @ApiOperation(value="根据店铺id和菜品id显示菜品的详细信息")
    @PostMapping("/selectByid")
    public CommonResult selectByid(Integer goodsid,Integer branchid, HttpServletRequest request){
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String id = user.getString("id");
        return iGoodsService.selectByid(goodsid,branchid,id);
    }
    @ApiOperation(value="根据店铺id和菜品id进行收藏")
    @GetMapping("/collect")
    public CommonResult collect(Integer goodsid, Integer branchid){
        return iGoodsService.collect(goodsid,branchid);
    }
    @ApiOperation(value="根据收藏id删除收藏")
    @GetMapping("/nocollect")
    public CommonResult nocollect( Integer id){
        return iGoodsService.nocollect(id);
    }

    @PostMapping("/selectType")
    public CommonResult selectType( Integer branchid){

        return iGoodsService.selectType1(branchid);
    }





}
